/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityprojet;

import entityprojet.exceptions.IllegalOrphanException;
import entityprojet.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author T.Ron
 */
public class SectionanalJpaController implements Serializable {

    public SectionanalJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    
    public SectionanalJpaController() {
        
    }
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("projetT_RPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Sectionanal sectionanal) {
        if (sectionanal.getCompteaCollection() == null) {
            sectionanal.setCompteaCollection(new ArrayList<Comptea>());
        }
        if (sectionanal.getPostebudgetaireCollection() == null) {
            sectionanal.setPostebudgetaireCollection(new ArrayList<Postebudgetaire>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Comptea> attachedCompteaCollection = new ArrayList<Comptea>();
            for (Comptea compteaCollectionCompteaToAttach : sectionanal.getCompteaCollection()) {
                compteaCollectionCompteaToAttach = em.getReference(compteaCollectionCompteaToAttach.getClass(), compteaCollectionCompteaToAttach.getIdCa());
                attachedCompteaCollection.add(compteaCollectionCompteaToAttach);
            }
            sectionanal.setCompteaCollection(attachedCompteaCollection);
            Collection<Postebudgetaire> attachedPostebudgetaireCollection = new ArrayList<Postebudgetaire>();
            for (Postebudgetaire postebudgetaireCollectionPostebudgetaireToAttach : sectionanal.getPostebudgetaireCollection()) {
                postebudgetaireCollectionPostebudgetaireToAttach = em.getReference(postebudgetaireCollectionPostebudgetaireToAttach.getClass(), postebudgetaireCollectionPostebudgetaireToAttach.getIdPb());
                attachedPostebudgetaireCollection.add(postebudgetaireCollectionPostebudgetaireToAttach);
            }
            sectionanal.setPostebudgetaireCollection(attachedPostebudgetaireCollection);
            em.persist(sectionanal);
            for (Comptea compteaCollectionComptea : sectionanal.getCompteaCollection()) {
                Sectionanal oldSaOfCompteaCollectionComptea = compteaCollectionComptea.getSa();
                compteaCollectionComptea.setSa(sectionanal);
                compteaCollectionComptea = em.merge(compteaCollectionComptea);
                if (oldSaOfCompteaCollectionComptea != null) {
                    oldSaOfCompteaCollectionComptea.getCompteaCollection().remove(compteaCollectionComptea);
                    oldSaOfCompteaCollectionComptea = em.merge(oldSaOfCompteaCollectionComptea);
                }
            }
            for (Postebudgetaire postebudgetaireCollectionPostebudgetaire : sectionanal.getPostebudgetaireCollection()) {
                Sectionanal oldSaOfPostebudgetaireCollectionPostebudgetaire = postebudgetaireCollectionPostebudgetaire.getSa();
                postebudgetaireCollectionPostebudgetaire.setSa(sectionanal);
                postebudgetaireCollectionPostebudgetaire = em.merge(postebudgetaireCollectionPostebudgetaire);
                if (oldSaOfPostebudgetaireCollectionPostebudgetaire != null) {
                    oldSaOfPostebudgetaireCollectionPostebudgetaire.getPostebudgetaireCollection().remove(postebudgetaireCollectionPostebudgetaire);
                    oldSaOfPostebudgetaireCollectionPostebudgetaire = em.merge(oldSaOfPostebudgetaireCollectionPostebudgetaire);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Sectionanal sectionanal) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Sectionanal persistentSectionanal = em.find(Sectionanal.class, sectionanal.getIdSa());
            Collection<Comptea> compteaCollectionOld = persistentSectionanal.getCompteaCollection();
            Collection<Comptea> compteaCollectionNew = sectionanal.getCompteaCollection();
            Collection<Postebudgetaire> postebudgetaireCollectionOld = persistentSectionanal.getPostebudgetaireCollection();
            Collection<Postebudgetaire> postebudgetaireCollectionNew = sectionanal.getPostebudgetaireCollection();
            List<String> illegalOrphanMessages = null;
            for (Comptea compteaCollectionOldComptea : compteaCollectionOld) {
                if (!compteaCollectionNew.contains(compteaCollectionOldComptea)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Comptea " + compteaCollectionOldComptea + " since its sa field is not nullable.");
                }
            }
            for (Postebudgetaire postebudgetaireCollectionOldPostebudgetaire : postebudgetaireCollectionOld) {
                if (!postebudgetaireCollectionNew.contains(postebudgetaireCollectionOldPostebudgetaire)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Postebudgetaire " + postebudgetaireCollectionOldPostebudgetaire + " since its sa field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Comptea> attachedCompteaCollectionNew = new ArrayList<Comptea>();
            for (Comptea compteaCollectionNewCompteaToAttach : compteaCollectionNew) {
                compteaCollectionNewCompteaToAttach = em.getReference(compteaCollectionNewCompteaToAttach.getClass(), compteaCollectionNewCompteaToAttach.getIdCa());
                attachedCompteaCollectionNew.add(compteaCollectionNewCompteaToAttach);
            }
            compteaCollectionNew = attachedCompteaCollectionNew;
            sectionanal.setCompteaCollection(compteaCollectionNew);
            Collection<Postebudgetaire> attachedPostebudgetaireCollectionNew = new ArrayList<Postebudgetaire>();
            for (Postebudgetaire postebudgetaireCollectionNewPostebudgetaireToAttach : postebudgetaireCollectionNew) {
                postebudgetaireCollectionNewPostebudgetaireToAttach = em.getReference(postebudgetaireCollectionNewPostebudgetaireToAttach.getClass(), postebudgetaireCollectionNewPostebudgetaireToAttach.getIdPb());
                attachedPostebudgetaireCollectionNew.add(postebudgetaireCollectionNewPostebudgetaireToAttach);
            }
            postebudgetaireCollectionNew = attachedPostebudgetaireCollectionNew;
            sectionanal.setPostebudgetaireCollection(postebudgetaireCollectionNew);
            sectionanal = em.merge(sectionanal);
            for (Comptea compteaCollectionNewComptea : compteaCollectionNew) {
                if (!compteaCollectionOld.contains(compteaCollectionNewComptea)) {
                    Sectionanal oldSaOfCompteaCollectionNewComptea = compteaCollectionNewComptea.getSa();
                    compteaCollectionNewComptea.setSa(sectionanal);
                    compteaCollectionNewComptea = em.merge(compteaCollectionNewComptea);
                    if (oldSaOfCompteaCollectionNewComptea != null && !oldSaOfCompteaCollectionNewComptea.equals(sectionanal)) {
                        oldSaOfCompteaCollectionNewComptea.getCompteaCollection().remove(compteaCollectionNewComptea);
                        oldSaOfCompteaCollectionNewComptea = em.merge(oldSaOfCompteaCollectionNewComptea);
                    }
                }
            }
            for (Postebudgetaire postebudgetaireCollectionNewPostebudgetaire : postebudgetaireCollectionNew) {
                if (!postebudgetaireCollectionOld.contains(postebudgetaireCollectionNewPostebudgetaire)) {
                    Sectionanal oldSaOfPostebudgetaireCollectionNewPostebudgetaire = postebudgetaireCollectionNewPostebudgetaire.getSa();
                    postebudgetaireCollectionNewPostebudgetaire.setSa(sectionanal);
                    postebudgetaireCollectionNewPostebudgetaire = em.merge(postebudgetaireCollectionNewPostebudgetaire);
                    if (oldSaOfPostebudgetaireCollectionNewPostebudgetaire != null && !oldSaOfPostebudgetaireCollectionNewPostebudgetaire.equals(sectionanal)) {
                        oldSaOfPostebudgetaireCollectionNewPostebudgetaire.getPostebudgetaireCollection().remove(postebudgetaireCollectionNewPostebudgetaire);
                        oldSaOfPostebudgetaireCollectionNewPostebudgetaire = em.merge(oldSaOfPostebudgetaireCollectionNewPostebudgetaire);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = sectionanal.getIdSa();
                if (findSectionanal(id) == null) {
                    throw new NonexistentEntityException("The sectionanal with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Sectionanal sectionanal;
            try {
                sectionanal = em.getReference(Sectionanal.class, id);
                sectionanal.getIdSa();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The sectionanal with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Comptea> compteaCollectionOrphanCheck = sectionanal.getCompteaCollection();
            for (Comptea compteaCollectionOrphanCheckComptea : compteaCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Sectionanal (" + sectionanal + ") cannot be destroyed since the Comptea " + compteaCollectionOrphanCheckComptea + " in its compteaCollection field has a non-nullable sa field.");
            }
            Collection<Postebudgetaire> postebudgetaireCollectionOrphanCheck = sectionanal.getPostebudgetaireCollection();
            for (Postebudgetaire postebudgetaireCollectionOrphanCheckPostebudgetaire : postebudgetaireCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Sectionanal (" + sectionanal + ") cannot be destroyed since the Postebudgetaire " + postebudgetaireCollectionOrphanCheckPostebudgetaire + " in its postebudgetaireCollection field has a non-nullable sa field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(sectionanal);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Sectionanal> findSectionanalEntities() {
        return findSectionanalEntities(true, -1, -1);
    }

    public List<Sectionanal> findSectionanalEntities(int maxResults, int firstResult) {
        return findSectionanalEntities(false, maxResults, firstResult);
    }

    private List<Sectionanal> findSectionanalEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Sectionanal.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Sectionanal findSectionanal(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Sectionanal.class, id);
        } finally {
            em.close();
        }
    }

    public int getSectionanalCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Sectionanal> rt = cq.from(Sectionanal.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
